// pages/system/system.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: '13856778523'
  },

  toCall() {
    const that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.phoneNumber // 仅为示例，并非真实的电话号码
    })
  },

  getUpdateManager() {
    const updateManager = wx.getUpdateManager();
    updateManager.onCheckForUpdate(function(res) {
      if (!res.hasUpdate) {
        app.showInfo('当前已经是最新版本')
      } else {
        that.setData({
          hiddenLoading: false
        })
        updateManager.onUpdateReady(function() {
          that.setData({
            hiddenLoading: true
          })
          wx.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            confirmText: '确定',
            confirmColor: '#ff5151',
            success(res) {
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate()
              }
            }
          })
        })
      }
    })
  },

  logout() {
    const that = this;
    wx.showModal({
      title: '',
      content: '是否确认退出',
      confirmText: '退出',
      confirmColor: '#ff5151',
      success(res) {
        if (res.confirm) {
          that.setData({
            hiddenLoading: false
          })
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          setTimeout(() => {
            that.setData({
              histories: [],
              hiddenLoading: true
            })
            let pages = getCurrentPages(); //当前页面栈
            let prevPage = pages[pages.length - 2]; //上一页面

            app.globalData={
              token: '',
              userInfo: null,
              systemInfo: '',
              location: '',
              address: null,
              userInfoAuthorization: false,
              locationAuthorization: false,
              classifyIndex: 0,
              cartCount: 0,
              tooFar: false
            },
            app.onLaunch();
            prevPage.setData({
              userInfoAuthorization: false,
              userInfo: null
            });
            wx.navigateBack({
              delta: -1,
              success(){
                // wx.removeTabBarBadge({
                //   index: 2,
                // })
              }
            })
          }, 400)
        } else {
          console.log('取消删除')
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    app.utils.post(app.api.reqUrls.getConfig, app.utils.getSignData({})).then(res => {
      that.setData({
        hiddenLoading: true
      })
      console.log(res)
      if(res.status == 1){
        that.setData({
          phoneNumber: res.result.config[0].value
        })
      }
    })
  },
  onShow: function () {
    //数据统计
    app.statistics("page/detail/pages/system/system", "系统设置")
  },

})